### This code serves to generate latex tables

import os

r_dir = r"E:\学习\2021秋\Uchi助研\Eman\March"
d_dir = r"E:\学习\2021秋\Uchi助研\Eman\March\test"


for j in range(0,11):
    target_1 = r_dir + r"\drafttab_lphetero_cut" + str(j) + r"0_ExpectedInterest.tex"
    #target_1 = r_dir + r"\drafttab_lphetero_cut" + str(j) + r"0_PartnerRating.tex"
    target_2 = r_dir + r"\drafttab_lphetero_cut" + str(j) + r"0_ExpectedInteresttest.tex"
    #target_2 = r_dir + r"\drafttab_lphetero_cut" + str(j) + r"0_PartnerRatingtest.tex"
    o1 = d_dir  + r"\drafttab_lphetero_cut" + str(j) + r"0_ExpectedInterestadd.tex"
    #o1 = d_dir  + r"\drafttab_lphetero_cut" + str(j) + r"0_PartnerRatingadd.tex"
    #o2 = d_dir + r"\table_lp_rating_rank.tex"
    #o3 = d_dir + r"\table_lp_expect_rank.tex"
    
    with open(target_1,"r", encoding="utf-8", errors="ignore") as f1:
        main_regress_raw = f1.read().splitlines()[8:66]
    
    with open(target_2,"r", encoding="utf-8", errors="ignore") as f1:
        test_p_raw = f1.read().splitlines()[9:27]
    
    
    main_regress = [t.split("&") for t in main_regress_raw]
    test_p = [t.split("&") for t in test_p_raw]
    
    
    ### start to make combinations, the aim is to generate three files o1 o2 and o3
    ###  main_regress  1 obs * 3 rows (45 rows in total)
    ### test_p 1 obs * 1 rows
    
    o1_output = []
    #o2_output = []
    #o3_output = []
    
    for i in range(len(main_regress)):
        ori_row = [t.replace("\\\\","") for t in main_regress[i]]
        if i//3 < len(test_p):
            p_row = [t.replace("\\\\","") for t in test_p[i//3]]
        print(ori_row)
        print(p_row)
        o1_row = []
        #o2_row = []
        #o3_row = []
        if (i+1)%3==0 and i <51:
            o1_output.append(ori_row)
            #o2_output.append(ori_row)
            #o3_output.append(ori_row)
            continue
        else:
            o1_row.append(ori_row[0])
            #o2_row.append(ori_row[0])
            #o3_row.append(ori_row[0])                
        ### a sequence of entry ###
        o1_row.extend(ori_row[1:3])  ### row 1 + 2
        #o2_row.extend(ori_row[5:7])  ### row 5 + 6
        #o3_row.extend(ori_row[9:11]) ### row 9 + 10
        if (i+1)%3==1:
            o1_row.append(p_row[1])  ### p-value if the title row
            #o2_row.append(p_row[3])
            #o3_row.append(p_row[5])        
        else:
            o1_row.append("")
            #o2_row.append("")
            #o3_row.append("")    
        ### a sequence of entry ###
        o1_row.extend(ori_row[3:5])  ### row 3 + 4
        #o2_row.extend(ori_row[7:9])  ### row 7 + 8
        #o3_row.extend(ori_row[11:13]) ### row 11 + 12
        if (i+1)%3==1:
            o1_row.append(p_row[2]+"\\\\")  ### p-value if the title row
            #o2_row.append(p_row[4]+"\\\\")
            #o3_row.append(p_row[6]+"\\\\")        
        else:
            o1_row.append("\\\\")
            #o2_row.append("\\\\")
            #o3_row.append("\\\\") 
        o1_output.append(o1_row)
        #o2_output.append(o2_row)
        #o3_output.append(o3_row)
    
    
    with open(o1,"w", encoding="utf-8", errors="ignore") as g1:
        for each_item in o1_output:
            print("&".join(each_item), file = g1)
            
    print(j)
    '''
    with open(o2,"w", encoding="utf-8", errors="ignore") as g1:
        for each_item in o2_output:
            print("&".join(each_item), file = g1)       
    with open(o3,"w", encoding="utf-8", errors="ignore") as g1:
        for each_item in o3_output:
            print("&".join(each_item), file = g1)    '''
        
    





